package view;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JTextField;

import model.FileParser;
import model.Manuscript;
import model.State;
import model.Status;

/**
 * Class to submit a paper given information in three text fields.
 * @author Sarah George
 * @version Autumn 2012
 */
@SuppressWarnings("serial")
public class SubmitPaperAction extends AbstractAction {
	
	/**
	 * Field to hold the current state of the system.
	 */
	private State my_state;
	
	/**
	 * Field to hold the JFrame of the submit paper functionality.
	 */
	private JFrame my_frame;
	
	/**
	 * Field to hold the JTextField that concerns the title.
	 */
	private JTextField title;
	
	/**
	 * Field to hold the JTextField that concerns the abstract.
	 */
	private JTextField abst;
	
	/**
	 * Field to hold the JTextField that concerns the file path.
	 */
	private JTextField path;
	
	/**
	 * Constructor for a submit paper action.
	 * @param the_state the current state of the system.
	 * @param the_frame the JFrame of the Submit New Paper GUI.
	 * @param tf the title of the paper.
	 * @param af the abstract of the paper.
	 * @param fpf the file path for the paper.
	 */
	public SubmitPaperAction(State the_state, JFrame the_frame, JTextField tf, JTextField af, 
							JTextField fpf) {
		super("Submit");
		putValue(Action.MNEMONIC_KEY, KeyEvent.VK_U);
		putValue(Action.SELECTED_KEY, true);
		my_state = the_state;
		my_frame = the_frame;
		title = tf;
		abst = af;
		path = fpf;
	}

	/**
	 * Method to submit a paper.
	 * @param arg0 the action event that triggers the method.
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		FileParser fp = my_state.getParser();
		Manuscript mscript = new Manuscript(fp.NEW_ELEMENT, my_state.getUser().getID(), Status.NEW, 
									my_state.getConference().getConfID(), title.getText(), 
									abst.getText(), path.getText(), -1);
		
		mscript.makeFile();
		fp.writeManuscript(mscript);
		my_frame.dispose();
	}
}//End class SubmitPaperAction